home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr34 / onetime.zip / TCODE.C < prev    next >
C/C++ Source or Header  |  1995-04-26  |  3KB  |  147 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4.  
  5. #define ALPHA_CONST 26
  6. #define GROUP 5
  7. #define SPACE ' '
  8. #define MAX_INPUT_LEN 30
  9. #define INVOCATION_ERROR 20
  10. #define FILE_ERR 11
  11. #define BUFFERSIZE 8192
  12. #define CMD *argv[1]
  13.  
  14. typedef enum { ENCODE, DECODE } Codeflag;
  15.  
  16. int convert( int ch, Codeflag C );
  17. long code( Codeflag Operation);
  18.  
  19.  
  20.  
  21.  
  22.  
  23. void main( int argc, char **argv )
  24. {
  25.    char answer[ MAX_INPUT_LEN ];
  26.  
  27.    if( CMD == 'e' || CMD == 'E' )
  28.      code( ENCODE );
  29.    else
  30.      if( CMD == 'd' || CMD == 'D' )
  31.         code ( DECODE );
  32.    else
  33.      {
  34.      puts( "Encode or Decode? ");
  35.      gets( answer );
  36.  
  37.      if( *answer == 'e' || *answer == 'E' )
  38.         code ( ENCODE );
  39.      else
  40.         if( *answer == 'd' || *answer == 'D' )
  41.           code ( DECODE );
  42.      else
  43.         exit ( INVOCATION_ERROR );
  44.  
  45.      }
  46.  
  47. }
  48.  
  49.  
  50.  
  51.  
  52. long code( Codeflag Operation)
  53. {
  54.  
  55.    register int c,
  56.              ck,
  57.              t;
  58.  
  59.    long number = 0L;  //Number of characters operated on.
  60.  
  61.    FILE *plaintxt,
  62.        *codekey,
  63.        *encfile,
  64.        *filename;
  65.  
  66.       if( NULL == ( codekey = fopen( "code.key", "r" ) ) )
  67.           exit( FILE_ERR );
  68.       if( setvbuf( codekey, NULL, _IOFBF, BUFFERSIZE ) )
  69.           exit( FILE_ERR );
  70.  
  71.    if( Operation == ENCODE )
  72.     {
  73.       if( NULL == ( plaintxt = fopen( "plain.txt", "r" ) ) )
  74.           exit( FILE_ERR );
  75.       if( setvbuf( plaintxt, NULL, _IOFBF, BUFFERSIZE ) )
  76.          exit( FILE_ERR );
  77.  
  78.       if( NULL == ( encfile = fopen( "encr.xxx", "a" ) ) )
  79.           exit ( FILE_ERR );
  80.       if( setvbuf( encfile, NULL, _IOFBF, BUFFERSIZE ) )
  81.          exit( FILE_ERR );
  82.     }
  83.    else
  84.     {
  85.       if( NULL == ( plaintxt = fopen( "decr.txt", "w" ) ) )
  86.          exit( FILE_ERR );
  87.       if( setvbuf( plaintxt, NULL, _IOFBF, BUFFERSIZE ) )
  88.          exit( FILE_ERR );
  89.  
  90.       if( NULL == ( encfile = fopen( "encr.xxx", "r" ) ) )
  91.          exit ( FILE_ERR );
  92.       if( setvbuf( encfile, NULL, _IOFBF, BUFFERSIZE ) )
  93.          exit( FILE_ERR );
  94.     }
  95.  
  96.   if( Operation == ENCODE )
  97.     filename = plaintxt;
  98.   else
  99.     filename = encfile;
  100.  
  101.    while( EOF != ( c = fgetc( filename ) ) )
  102.      if( isalpha( c ) )
  103.        {
  104.        number++;
  105.  
  106.        c = tolower( c );
  107.        ck = fgetc( codekey );
  108.  
  109.        if( Operation == ENCODE )
  110.          c += ck;
  111.        else
  112.          c -= ck;
  113.  
  114.        t = convert( c, Operation );
  115.  
  116.        if( Operation == ENCODE )
  117.          fputc( t, encfile );
  118.        else
  119.          fputc( t, plaintxt );
  120.  
  121.        if( !( number % GROUP ) && Operation == ENCODE )
  122.          fputc( SPACE, encfile );
  123.  
  124.        }
  125.  
  126.    fcloseall();
  127.  
  128.    return( number );
  129.  
  130. }
  131.  
  132. int convert( int ch, Codeflag C )
  133. {
  134.  
  135.  
  136.      while ( !islower( ch ) )
  137.         if( C == ENCODE )
  138.           ch -= ALPHA_CONST;
  139.         else
  140.           ch += ALPHA_CONST;
  141.  
  142.  
  143.       return( ch );
  144.  
  145. }
  146.  
  147.